import request from './axios';

/**
 * 获取所有订单（分页）
 * @param page 页码
 * @param size 每页大小
 * @param sort 排序字段
 * @param direction 排序方向 asc或desc
 */
export function getOrdersPage(page = 0, size = 10, sort = 'orderDate', direction = 'desc') {
  return request({
    url: '/order/page',
    method: 'get',
    params: { page, size, sort, direction }
  });
}

/**
 * 创建订单
 * @param orderData 订单数据
 */
export function createOrder(orderData: any) {
  return request({
    url: '/order',
    method: 'post',
    data: orderData
  });
}

/**
 * 获取订单详情
 * @param id 订单ID
 */
export function getOrderById(id: number) {
  return request({
    url: `/order/${id}`,
    method: 'get'
  });
}

/**
 * 获取订单详情（通过订单号）
 * @param orderNumber 订单号
 */
export function getOrderByOrderNumber(orderNumber: string) {
  return request({
    url: `/order/number/${orderNumber}`,
    method: 'get'
  });
}

/**
 * 按状态获取订单
 * @param status 订单状态
 * @param page 页码
 * @param size 每页大小
 * @param sort 排序字段
 * @param direction 排序方向
 */
export function getOrdersByStatus(status: string, page = 0, size = 10, sort = 'orderDate', direction = 'desc') {
  return request({
    url: `/order/status/${status}/page`,
    method: 'get',
    params: { page, size, sort, direction }
  });
}

/**
 * 按日期范围获取订单
 * @param start 开始日期
 * @param end 结束日期
 * @param page 页码
 * @param size 每页大小
 * @param sort 排序字段
 * @param direction 排序方向
 */
export function getOrdersByDateRange(start: string, end: string, page = 0, size = 10, sort = 'orderDate', direction = 'desc') {
  return request({
    url: '/order/dateRange',
    method: 'get',
    params: { 
      start,
      end,
      page, 
      size, 
      sort, 
      direction 
    }
  });
}

/**
 * 搜索订单
 * @param keyword 关键词
 * @param page 页码
 * @param size 每页大小
 * @param sort 排序字段
 * @param direction 排序方向
 */
export function searchOrders(keyword: string, page = 0, size = 10, sort = 'orderDate', direction = 'desc') {
  return request({
    url: '/order/search',
    method: 'get',
    params: { 
      keyword,
      page, 
      size, 
      sort, 
      direction 
    }
  });
}

/**
 * 更新订单状态
 * @param id 订单ID
 * @param status 新状态
 */
export function updateOrderStatus(id: number, status: string) {
  return request({
    url: `/order/${id}/status`,
    method: 'put',
    params: { status }
  });
}

/**
 * 更新物流单号
 * @param id 订单ID
 * @param trackingNumber 物流单号
 */
export function updateTrackingNumber(id: number, trackingNumber: string) {
  return request({
    url: `/order/${id}/tracking`,
    method: 'put',
    params: { trackingNumber }
  });
}

/**
 * 根据客户ID获取订单列表
 * @param customerId 客户ID
 * @param page 页码
 * @param size 每页大小
 * @param sort 排序字段
 * @param direction 排序方向
 */
export function getOrdersByCustomerId(customerId: string, page = 0, size = 10, sort = 'orderDate', direction = 'desc') {
  return request({
    url: `/order/customer/${customerId}/page`,
    method: 'get',
    params: { page, size, sort, direction }
  });
}

/**
 * 高级搜索订单
 */
export function advancedSearchOrders(params: any) {
  return request({
    url: '/order/advancedSearch',
    method: 'get',
    params
  });
}

/**
 * 获取可用物流承运商
 */
export function getAvailableCarriers() {
  return request({
    url: '/order/available-carriers',
    method: 'get'
  });
}

/**
 * 为订单创建物流单
 * @param id 订单ID
 * @param carrierName 承运商名称
 */
export function createOrderShipment(id: number, carrierName: string) {
  return request({
    url: `/order/${id}/shipment`,
    method: 'post',
    params: { carrierName }
  });
} 